// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import BulletScript from "./BulletScript";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {


  // LIFE-CYCLE CALLBACKS:
  @property(cc.SpriteFrame)
  bulletIcon: cc.SpriteFrame = null // 子弹图片

  @property(cc.SpriteFrame)
  explodeEffect: cc.SpriteFrame = null // 爆炸图片

  direction: number = 0 // 方向

  // onLoad () {}

  start () {
    this.node.on('touchstart', this.fire, this)
  }

  /**
   * 发射子弹
   */
  fire () {
    if (!this.bulletIcon) return false
    // 动态创建NODE，添加Sprite组件
    let bullet: cc.Node = new cc.Node()
    let sprite: cc.Sprite = bullet.addComponent(cc.Sprite)
    sprite.spriteFrame = this.bulletIcon
    
    bullet.parent = this.node // 此节点挂载到当前节点之后

    bullet.setPosition(cc.v3(0, 80, 0)) // 初始化位置

    // 加挂脚本组件
    let script: BulletScript = bullet.addComponent(BulletScript)
    script.explodeEffect = this.explodeEffect
  }

  update (dt) {
    if (this.direction === 0) {
      this.node.angle = 0
      return false
    }
    this.node.angle = this.direction
  }
}
